// 厦门大学计算机专业 | 前华为工程师
// 专注《零基础学编程系列》https://cxyxy.blog.csdn.net/article/details/121134634
// 包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
// 公众号：蓝不蓝编程

import SwiftUI
import Combine

struct ContentView: View {
    private let symbolActive = "play.circle.fill"
    private let symbolInactive = "pause.circle.fill"
    
    @State private var isChanging = false
    @State private var ball1 = 1
    @State private var ball2 = 1
    @State private var ball3 = 1
    @State private var ball4 = 1
    @State private var ball5 = 1
    @State private var ball6 = 1
    @State private var ball7 = 1
    
    private let timeHelper = TimeHelp()
    
    var body: some View {
        VStack {
            Text("双色球-蓝不蓝编程").padding()
            Spacer()
            HStack{
                getRedBall(ballNum: ball1)
                getRedBall(ballNum: ball2)
                getRedBall(ballNum: ball3)
                getRedBall(ballNum: ball4)
                getRedBall(ballNum: ball5)
                getRedBall(ballNum: ball6)
                getBlueBall(ballNum: ball7)
            }
            Spacer()
            Button(action: {
                if(isChanging){
                    self.timeHelper.stop()
                }else{
                    self.timeHelper.start {changeBalls()}
                }
                isChanging.toggle()
            }, label: {
                Image(systemName: isChanging ? symbolInactive : symbolActive)
                    .font(.system(size: 80))
            }).padding()
            Spacer()
        }
    }
    
    fileprivate func changeBalls() {
        ball1 = getRedBallNum()
        ball2 = getRedBallNum()
        ball3 = getRedBallNum()
        ball4 = getRedBallNum()
        ball5 = getRedBallNum()
        ball6 = getRedBallNum()
        ball7 = getBlueBallNum()
    }
    
    fileprivate func getRedBallNum() -> Int {
        return Int(arc4random_uniform(33))
    }
    
    fileprivate func getBlueBallNum() -> Int {
        return Int(arc4random_uniform(16))
    }
    
    fileprivate func getRedBall(ballNum:Int) -> some View {
        return Text("\(formatNum(from: ballNum))")
            .foregroundColor(.white)
            .frame(width: 40, height: 40)
            .background(Circle().fill(Color.red).frame(width: 40, height: 40))
    }
    
    fileprivate func getBlueBall(ballNum:Int) -> some View {
        return Text("\(formatNum(from: ballNum))")
            .foregroundColor(.white)
            .frame(width: 40, height: 40)
            .background(Circle().fill(Color.blue).frame(width: 40, height: 40))
    }
    
    private func formatNum(from ballNum: Int) -> String {
        return String(format:"%02i", ballNum)
    }

}

class TimeHelp {
    
    var canceller: AnyCancellable?
        
    //每次都新建一个计时器
    func start(receiveValue: @escaping (() -> Void)) {
        let timerPublisher = Timer
            .publish(every: 0.1, on: .main, in: .common)
            .autoconnect()
        
        self.canceller = timerPublisher.sink { date in
            receiveValue()
        }
    }
    
    //暂停销毁计时器
    func stop() {
        canceller?.cancel()
        canceller = nil
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
